goog.events.listen( document.body, goog.events.EventType.CLICK, function( e )
{
    var el = (e).target;

    if ( el.tagName.toLowerCase() == 'button' && el.getAttribute( "h" ) )
    {
        goog.dom.classes.add( el, "waiting" );

        var data = goog.Uri.QueryData.createFromMap( new goog.structs.Map( {
            "id": el.getAttribute( "id" ),
            "__method": el.getAttribute( "ajax-method" ),
            "__ajax_handler": el.getAttribute( "h" )
        } ) );

        var xhr = new goog.net.XhrIo();
        goog.events.listen( xhr, goog.net.EventType.SUCCESS, function( e )
        {
            try
            {
                var response = e.target.getResponseJson();
                if ( response["hasErrors"] )
                {
                    handleMessages( response );
                } else if ( response["responseType"] == "REDIRECT" )
                {
                    document.location.href = response["value"];
                }
            }
            catch ( err )
            {
                if ( console )
                {
                    console.error( err );
                }
            }

            goog.dom.classes.remove( el, "waiting" );
        } );

        // set ajax header
        var headers = new goog.structs.Map();
        headers.set( "X-Requested-With", "XMLHttpRequest" );

        // send request
        xhr.send( new goog.Uri( document.location.href ).getPath(), "POST", data.toString(), headers );

        e.preventDefault();
    }
} );